import { Module } from 'vuex'
import { PaymentState, RootState } from '../types'
import { PaymentService } from '@/api/payment'

const paymentModule: Module<PaymentState, RootState> = {
  namespaced: true,
  
  state: {
    loading: false
  },
  
  mutations: {
    SET_LOADING(state, loading: boolean) {
      state.loading = loading
    }
  },
  
  actions: {
    async createPayment({ commit }, { orderId, method }) {
      commit('SET_LOADING', true)
      try {
        const response = await PaymentService.createPayment(orderId, method)
        return response.data
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async checkPaymentStatus({ commit }, orderId) {
      const response = await PaymentService.checkPaymentStatus(orderId)
      return response.data.status
    }
  }
}

export default paymentModule 